<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5                                                          |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | This source file is subject to version 3.00 of the PHP License,        |
// | that is available at http://www.php.net/license/3_0.txt.               |
// | If you did not receive a copy of the PHP license and are unable to     |
// | obtain it through the world-wide-web, please send a note to            |
// | license@php.net so we can mail you a copy immediately.                 |
// +------------------------------------------------------------------------+
// | Author:                                                                |
// +------------------------------------------------------------------------+
//
// $Id$
//


/**
 * @author       mk
 */
class Loader {
    
    /**
     * @var      array
     */
    protected static $classPath;
    protected static $cacheFileName = '/cache/class_path.php';
    
    
    /**
     * @param    string $ClassName    
     * @return   void
     */
    static public function load($ClassName) {
        if (empty(self::$classPath)) {
            self::initIncludePath();
            if (!is_readable(ROOT . self::$cacheFileName)) {
                self::initCacheFile();
            }
            self::$classPath = include(ROOT . self::$cacheFileName);
        }
        
        foreach (self::$classPath as $class => $file) {
            if ($class == $ClassName) {
                include_once($file);
                break;
            }
        }
    }
    
    /**
     * @return   void
     */
    static public function initIncludePath() {
        set_include_path(
            ROOT.'/libs/core'.PATH_SEPARATOR.
            ROOT.'/libs/vendor'.PATH_SEPARATOR.
            ROOT.'/libs/vendor/PEAR');
    }
    
    /**
     * @return   void
     */
    static public function initCacheFile() {
        include_once('File/Find.php');
        
        $cache = fopen(ROOT . self::$cacheFileName, 'w');
        fwrite($cache, "<?php\n\n");
        fwrite($cache, "// author mk\n");
        fwrite($cache, "return array(\n\n");
        $dirs = array('config', 'libs', 'modules','erp');
        //$classPath = array();
        
        foreach ($dirs as $dir) {
            $files = File_Find::search('~.*\.php$~i', ROOT.DIRECTORY_SEPARATOR.$dir, 'perl');
            foreach ($files as $file) {
                preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi', file_get_contents($file), $classes);
                foreach ($classes[1] as $class) {
                    fwrite($cache, "'$class' => '$file',\n");
                    //$classPath[$class] = $file;
                }
            }
        }
        fwrite($cache, ");\n\n");
        //print_r($classPath);
    }
}

